<?php
namespace app\admin\controller;
use think\Db;
class Express
{
    public function __construct(){
        Admin_auth();
    }

    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $pages = pages('express',$where,input('get.page'));
        $data = Db::table('express')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data' => $data,'pages'=>$pages]);
    }

    public function edit()
    {
        $id = input('get.id')?input('get.id'):0;
        if(request()->isPost()){
            $data = input('post.');
            if(!$data['aid']){
                echo '请选择城市';
            }else{
				$data['cid'] = Db::table('area')->where(['id' => $data['aid']])->value("father_id");
				$data['pid'] = Db::table('city')->where(['id' => $data['cid']])->value("father_id");
                if(Db::table('express')->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
                    return error(lang('tip')['re_phone']);
                }
                if($id){
                    if(empty($data['password'])){
                        unset($data['password']);
                    }else{
                        $data['password'] = md5($data['password']);
                    }
                    Db::table('express')->where(['id' => $id])->update($data);
                    $data['id'] = $id;
                    $data['password'] = '';
                }else{
                    $data['password'] = md5($data['password']);
                    $data['status'] = 1;
                    $data['id'] = Db::table('express')->insertGetId($data);
                }
            }
        }else{
            $data = Db::table('express')->where(['id' => $id])->find();
        }
        return view('',['data'  => $data]);
    }

    public function del(){
        $id = input('get.id')?input('get.id'):0;
        $page = input('get.page')?input('get.page'):1;
        Db::table('express')->whereIn('aid',session('aid'))->delete($id);
        redirect(url('/admin/express?page='.$page));
        exit;
    }

    public function setting()
    {
        $pid = request()->request('pid');
        $cid = request()->request('cid');
        $aid = request()->request('aid');
        $data = [];
        if ($aid) {
            if (request()->isPost()) {
                $data = request()->post();
                Db::table("express_config")->where('aid', $aid)->update($data);
            }else {
                $count = Db::table('express_config')->where(['aid'=>$aid])->count();
                if($count == 0){
                    Db::table('express_config')->insert(['pid'=>$pid,'cid'=>$cid,'aid'=>$aid,'scope'=>3,'charge'=>'2','add_charge'=>2]);
                }
            }
            $data = Db::table('express_config')->where('aid', $aid)->find();
        }
        return view('',['data'=>$data]);
    }

    public function losemoney()
    {
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $where['type'] = 'express';
        $pages = pages('losemoney',$where,input('get.page'));
        $data = Db::table('losemoney')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        foreach ($data as $k => $v){
            $data[$k]['username'] = Db::table('user')->where('id',$v['uid'])->value('name');
        }
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }

    public function losemoney_edit()
    {
        $id = request()->request('id');
        $page = input('get.page');
        $uid = request()->request('uid');
        $phone = Db::table('express')->where('id',$uid)->value('phone');
        Db::table('losemoney')->where('id',$id)->update(['status'=>1]);
        $money = Db::table('losemoney')->where('id',$id)->find();
        Db::table('express')->where('id',$uid)->setDec('losemoney',$money['money']);
        push(md5($uid),json_encode(['title'=>'提现处理','message'=>'您的提现已成功打款，请注意查收','type'=>1]),'express');
        redirect(url('express/losemoney').'?page='.$page);
        exit;
    }

    public function losemoney_del()
    {
        $id = input('get.id')?input('get.id'):0;
        $page = input('get.page');
        $uid = request()->request('uid');
        $user = Db::table('express')->where('id',$uid)->find();
        $phone = $user['phone'];
        $money = $user['money']+$user['losemoney'];
        $losemoney = 0;
        Db::table('express')->where('id',$user['id'])->update(['money'=>$money,'losemoney'=>$losemoney]);
        push(md5($uid),json_encode(['title'=>'提现处理','message'=>'您的提现由于某种原因被拒绝','type'=>1]),'express');
        Db::table('losemoney')->where('id',$id)->update(['status'=>3]);
        redirect(url('express/losemoney').'?page='.$page);
        exit;
    }
}